import os
from openpyxl import load_workbook


"""
实现功能：啊
    按照指定格式批量命名
"""

# original_path = 'D:/pythonProject/测试/05_评审表扫描件/'
original_path = 'D:/pythonProject/测试/01_学士学位论文终稿/'
look_up_table_path = 'D:/pythonProject/测试/Name_Number.xlsx'
look_up_table_row_start = 2
look_up_table_row_number = 32

# 读取excel，匹配学号：姓名
name_number_dict = {}
number_lunwen_dict = {}
look_up_table_excel = load_workbook(look_up_table_path)
look_up_table_all_sheet = look_up_table_excel.get_sheet_names()
look_up_table_sheet = look_up_table_excel.get_sheet_by_name(look_up_table_all_sheet[0])
for i in range(look_up_table_row_start, look_up_table_row_start + look_up_table_row_number):
    number = look_up_table_sheet.cell(i, 1).value
    name = look_up_table_sheet.cell(i, 2).value
    lunwen = look_up_table_sheet.cell(i, 3).value
    name_number_dict[number] = name
    number_lunwen_dict[number] = lunwen

# Replacement Renaming

# print(name_number_dict)
# print(number_lunwen_dict)


all_word = os.listdir(original_path)
for i in range(len(all_word)):
    # old_name = all_word[i]
    # old_name_name = old_name[0:-4] #提取出姓名

    # -----------------------------------------------
    # 论文评审表替换(第一种方法)
    # new_name_number = [k for k, v in name_number_dict.items() if v == old_name_name]
    # print(new_name_number)
    # new_name = old_name.replace(old_name_name, '论文评审表_'+''.join(str(w) for w in new_name_number)+'_'+old_name_name)
    # print(new_name)
    # os.rename(original_path + old_name, original_path + new_name)
    # -----------------------------------------------

    # -----------------------------------------------
    # 论文评审表替换(第二种方法)
    # new_name = ''
    # for key, value in name_number_dict.items():
    #     if old_name_name == value:
    #         # new_name = '论文评审表_'+key+'_'+value
    #         # 论文名称替换(第二种方法) 学号_学生姓名_论文题目_专业名称
    #         new_name = key+'_'+value+'_'
    # if new_name :
    #     print(new_name)
    #     os.rename(original_path + old_name, original_path + new_name)
    # -----------------------------------------------

    # -----------------------------------------------
    old_name = all_word[i]
    old_name_name = old_name.split(".")[0]  # 提取出姓名
    old_name_geshi = old_name.split(".")[1]  # 提取出姓名
    print(old_name)
    print(old_name_name)
    print(old_name_geshi)
    # 论文替换方法
    new_name = ''
    for key, value in name_number_dict.items():
        if old_name_name == value:
            # new_name = '论文评审表_'+key+'_'+value
            # 论文名称替换(第二种方法) 学号_学生姓名_论文题目_专业名称
            new_name = key + '_' + value + '_'+number_lunwen_dict[key]+'.'+old_name_geshi
    if new_name:
        print(new_name)
        os.rename(original_path + old_name, original_path + new_name)